package io.renren.modules.course.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import io.renren.common.exception.RRException;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.oss.entity.MysqlOssEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.course.entity.WelfareEntity;
import io.renren.modules.course.service.WelfareService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 公司福利
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2018-08-09 08:55:58
 */
@RestController
@RequestMapping("course/welfare")
public class WelfareController {
    @Autowired
    private WelfareService welfareService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("course:welfare:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = welfareService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{welfareId}")
    @RequiresPermissions("course:welfare:info")
    public R info(@PathVariable("welfareId") Integer welfareId){
        WelfareEntity welfare = welfareService.selectById(welfareId);

        return R.ok().put("welfare", welfare);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("course:welfare:save")
    public R save(@RequestBody WelfareEntity welfare){
        welfareService.insert(welfare);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("course:welfare:update")
    public R update(@RequestBody WelfareEntity welfare){
        ValidatorUtils.validateEntity(welfare);
        welfareService.updateAllColumnById(welfare);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("course:welfare:delete")
    public R delete(@RequestBody Integer[] welfareIds){
        welfareService.deleteBatchIds(Arrays.asList(welfareIds));

        return R.ok();
    }

    /**
     * 上传文件
     */
    @RequestMapping("/welfareUpload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        String fileName = file.getOriginalFilename().substring(0,3);
        try {
            Map<String, Object> param = new HashMap<String,Object>();
            param.put("name",fileName);
            param.put("buffer",file.getBytes());
            welfareService.upload(param);
        } catch (Exception e) {
            System.out.println(e);
        }
        return R.ok().put("name",fileName);

    }

    /**
     * 读取文件
     */
    @RequestMapping("/getimage/{name}")
    public @ResponseBody
    Map<String,Object>  getimage(@PathVariable("name")String name, HttpServletResponse response) throws Exception {
        MysqlOssEntity m=welfareService.getImgByName(name);
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + m.getName());
        ServletOutputStream toClient=response.getOutputStream(); //得到向客户端输出二进制数据的对象
        toClient.write(m.getImg());  //输出数据
        toClient.flush();
        toClient.close();
        return null;
    }

}
